package com.book.controller;

import com.book.pojo.Book;
import com.book.pojo.com.book.entity.Code;
import com.book.pojo.com.book.entity.Result;
import com.book.service.BookService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 * @author xuchangshuo
 * @date 2020/10/29 15:11
 */
@RequestMapping("/book")
@Controller
public class BookController {

    @Autowired
    private BookService bookService;

    //查询所有图书信息
    @RequestMapping("/findAll")
    @ResponseBody
    public Result findAll() {
        List bookList = bookService.findAllBook();

        return new Result(Code.SUCCESS_CODE, bookList);
    }

    //添加图书
    @RequestMapping("/add")
    @ResponseBody
    public Result addBook(@RequestBody Book book) {
        boolean b = bookService.addBook(book);
        if (b) {
            return new Result(Code.SUCCESS_CODE, "添加成功");
        }
        return new Result(Code.FAILED_CODE, "添加失败");
    }

    //编辑图书
    @ResponseBody
    @RequestMapping("/edit")
    public Result editBook(@RequestBody Book book) {
        boolean b = bookService.editBook(book);
        if (b) {
            return new Result(Code.SUCCESS_CODE, "编辑成功");
        }
        return new Result(Code.FAILED_CODE, "编辑失败");
    }

    //删除图书
    @RequestMapping("/delete")
    @ResponseBody
    public Result delete(@RequestParam() int id) {
        System.out.println(id);
        boolean b = bookService.delete(id);
        if (b) {
            return new Result(Code.SUCCESS_CODE, "删除成功");
        }
        return new Result(Code.FAILED_CODE, "删除失败");
    }

    //分页查询
    @RequestMapping("/findPage")
    @ResponseBody
    public Result findPage(@RequestParam int currentPage, @RequestParam int pageSize) {
        Page<Book> page = bookService.findPage(currentPage, pageSize);
        List<Book> result = page.getResult();
//        System.out.println(result);

        return new Result(Code.SUCCESS_CODE, result);
    }

    //条件查询
    @RequestMapping("/search")
    @ResponseBody
    public Result search(@RequestBody Map<String, String> map) {
        List<Book> search = bookService.search(map);
        return new Result(Code.SUCCESS_CODE, search);
    }

    @RequestMapping("/getRedisData")
    public void getRedisData(){
        bookService.getRedisData();
        System.out.println(111111);
    }
}
